package nio.tcp.server_client;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class SocketClient {

    @SuppressWarnings("resource")
    public static void main(String[] args) throws UnknownHostException, IOException {

        try (Socket s = new Socket("127.0.0.1", 8888)) {

            InputStream inStream = s.getInputStream();
            OutputStream outStream = s.getOutputStream();

            PrintWriter out = new PrintWriter(outStream);
            out.println("你好！");
            out.flush();
            s.shutdownOutput();

            Scanner in = new Scanner(inStream);
            StringBuilder sb = new StringBuilder();
            while (in.hasNextLine()) {
                String line = in.nextLine();
                sb.append(line);
            }
            String response = sb.toString();
            System.out.println("response=" + response);
        }
    }
}
